//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.point.service.impl;

import ciicos.base.model.filter.RuleInfo;
import ciicos.model.visiual.superQuery.ConditionJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryConditionModel;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.permission.model.authorize.AuthorizeConditionModel;
import ciicos.permission.service.AuthorizeService;
import ciicos.permission.util.GenUtil;
import ciicos.point.entity.PointUpdateRecordEntity;
import ciicos.point.mapper.PointUpdateRecordMapper;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordForm;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordPagination;
import ciicos.point.service.PointUpdateRecordService;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.ServletUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PointUpdateRecordServiceImpl extends ServiceImpl<PointUpdateRecordMapper, PointUpdateRecordEntity> implements PointUpdateRecordService {
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private AuthorizeService authorizeService;

    public List<PointUpdateRecordEntity> getList(PointUpdateRecordPagination pointUpdateRecordPagination) {
        return this.getTypeList(pointUpdateRecordPagination, pointUpdateRecordPagination.getDataType());
    }

    public List<PointUpdateRecordEntity> getTypeList(PointUpdateRecordPagination pointUpdateRecordPagination, String dataType) {
        String userId = this.userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        List<List<String>> intersectionList = new ArrayList();
        int total = 0;
        int pointUpdateRecordNum = 0;
        QueryWrapper<PointUpdateRecordEntity> pointUpdateRecordQueryWrapper = new QueryWrapper();
        List<String> allSuperIDlist = new ArrayList();
        String superOp = "";
        if (ObjectUtil.isNotEmpty(pointUpdateRecordPagination.getSuperQueryJson())) {
            SuperQueryJsonModel superQueryJsonModel = (SuperQueryJsonModel)JsonUtil.getJsonToBean(pointUpdateRecordPagination.getSuperQueryJson(), SuperQueryJsonModel.class);
            String matchLogic = superQueryJsonModel.getMatchLogic();
            List<ConditionJsonModel> superQueryList = JsonUtil.getJsonToList(superQueryJsonModel.getConditionJson(), ConditionJsonModel.class);

            for(ConditionJsonModel conditionjson : superQueryList) {
                Map<String, Object> map = JsonUtil.stringToMap(conditionjson.getAttr());
                Map<String, Object> configMap = JsonUtil.stringToMap(map.get("__config__").toString());
                String tableName = configMap.get("relationTable") != null ? String.valueOf(configMap.get("relationTable")) : String.valueOf(configMap.get("tableName"));
                if (map.get("multiple") != null && Boolean.valueOf(String.valueOf(map.get("multiple"))) && ObjectUtil.isNull(conditionjson.getFieldValue())) {
                    conditionjson.setFieldValue("[]");
                }

                conditionjson.setTableName(tableName);
            }

            List<String> allSuperList = new ArrayList();
            List<List<String>> intersectionSuperList = new ArrayList();
            int pointUpdateRecordNum1 = 0;
            QueryWrapper<PointUpdateRecordEntity> pointUpdateRecordSuperWrapper = new QueryWrapper();
            pointUpdateRecordNum1 = this.generaterSwapUtil.getCondition(new SuperQueryConditionModel(pointUpdateRecordSuperWrapper, superQueryList, matchLogic, "point_update_record"), new PointUpdateRecordEntity(), pointUpdateRecordNum1);
            if (pointUpdateRecordNum1 > 0) {
                List<String> pointUpdateRecordList = (List)this.list(pointUpdateRecordSuperWrapper).stream().map(PointUpdateRecordEntity::getId).collect(Collectors.toList());
                allSuperList.addAll(pointUpdateRecordList);
                intersectionSuperList.add(pointUpdateRecordList);
            }

            superOp = matchLogic;
            if (matchLogic.equalsIgnoreCase("and")) {
                allSuperIDlist = this.generaterSwapUtil.getIntersection(intersectionSuperList);
            } else {
                allSuperIDlist = allSuperList;
            }
        }

        boolean pcPermission = true;
        boolean appPermission = false;
        boolean isPc = ServletUtil.getHeader("ciicos-origin").equals("pc");
        if (isPc && pcPermission && !this.userProvider.get().getIsAdministrator()) {
            Object pointUpdateRecordObj = this.authorizeService.getCondition(new AuthorizeConditionModel(pointUpdateRecordQueryWrapper, pointUpdateRecordPagination.getMenuId(), "point_update_record"));
            if (ObjectUtil.isEmpty(pointUpdateRecordObj)) {
                return new ArrayList();
            }

            pointUpdateRecordQueryWrapper = (QueryWrapper)pointUpdateRecordObj;
            if (pointUpdateRecordQueryWrapper.getExpression().getNormal().size() > 0) {
                ++pointUpdateRecordNum;
            }
        }

        if (!isPc && appPermission && !this.userProvider.get().getIsAdministrator()) {
            Object pointUpdateRecordObj = this.authorizeService.getCondition(new AuthorizeConditionModel(pointUpdateRecordQueryWrapper, pointUpdateRecordPagination.getMenuId(), "point_update_record"));
            if (ObjectUtil.isEmpty(pointUpdateRecordObj)) {
                return new ArrayList();
            }

            pointUpdateRecordQueryWrapper = (QueryWrapper)pointUpdateRecordObj;
            if (pointUpdateRecordQueryWrapper.getExpression().getNormal().size() > 0) {
                ++pointUpdateRecordNum;
            }
        }

        if (isPc) {
            if (ObjectUtil.isNotEmpty(pointUpdateRecordPagination.getSource())) {
                ++pointUpdateRecordNum;
                List<String> idList = new ArrayList();

                try {
                    String[][] source = (String[][])JsonUtil.getJsonToBean(pointUpdateRecordPagination.getSource(), String[][].class);

                    for(int i = 0; i < source.length; ++i) {
                        if (source[i].length > 0) {
                            idList.add(source[i][source[i].length - 1]);
                        }
                    }
                } catch (Exception var21) {
                    try {
                        List<String> source = JsonUtil.getJsonToList(pointUpdateRecordPagination.getSource(), String.class);
                        if (source.size() > 0) {
                            idList.add(source.get(source.size() - 1));
                        }
                    } catch (Exception var20) {
                        idList.add(String.valueOf(pointUpdateRecordPagination.getSource()));
                    }
                }

                pointUpdateRecordQueryWrapper.lambda().and((t) -> idList.forEach((tt) -> ((LambdaQueryWrapper)t.like(PointUpdateRecordEntity::getSource, tt)).or()));
            }

            if (ObjectUtil.isNotEmpty(pointUpdateRecordPagination.getPointnmae())) {
                ++pointUpdateRecordNum;
                pointUpdateRecordQueryWrapper.lambda().like(PointUpdateRecordEntity::getPointnmae, pointUpdateRecordPagination.getPointnmae());
            }

            if (ObjectUtil.isNotEmpty(pointUpdateRecordPagination.getCreatoruserid())) {
                ++pointUpdateRecordNum;
                pointUpdateRecordQueryWrapper.lambda().eq(PointUpdateRecordEntity::getCreatoruserid, pointUpdateRecordPagination.getCreatoruserid());
            }
        }

        List<String> intersection = this.generaterSwapUtil.getIntersection(intersectionList);
        if (total > 0) {
            if (intersection.size() == 0) {
                intersection.add("jnpfNullList");
            }

            pointUpdateRecordQueryWrapper.lambda().in(PointUpdateRecordEntity::getId, intersection);
        }

        if (StringUtil.isNotEmpty(superOp)) {
            if (allSuperIDlist.size() == 0) {
                allSuperIDlist.add("jnpfNullList");
            }
            List<String> finalAllSuperIDlist = allSuperIDlist;
            pointUpdateRecordQueryWrapper.lambda().and(t ->
                t.in(PointUpdateRecordEntity::getId, finalAllSuperIDlist)
            );
        }

        if (StringUtil.isEmpty(pointUpdateRecordPagination.getSidx())) {
            pointUpdateRecordQueryWrapper.lambda().orderByDesc(PointUpdateRecordEntity::getCreatortime);
        } else {
            try {
                String sidx = pointUpdateRecordPagination.getSidx();
                PointUpdateRecordEntity pointUpdateRecordEntity = new PointUpdateRecordEntity();
                Field declaredField = pointUpdateRecordEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = ((TableField)declaredField.getAnnotation(TableField.class)).value();
                pointUpdateRecordQueryWrapper = "asc".equals(pointUpdateRecordPagination.getSort().toLowerCase()) ? (QueryWrapper)pointUpdateRecordQueryWrapper.orderByAsc(value) : (QueryWrapper)pointUpdateRecordQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        if (!"0".equals(dataType)) {
            return this.list(pointUpdateRecordQueryWrapper);
        } else if ((total <= 0 || AllIdList.size() <= 0) && total != 0) {
            List<PointUpdateRecordEntity> list = new ArrayList();
            return pointUpdateRecordPagination.setData(list, (long)list.size());
        } else {
            Page<PointUpdateRecordEntity> page = new Page(pointUpdateRecordPagination.getCurrentPage(), pointUpdateRecordPagination.getPageSize());
            IPage<PointUpdateRecordEntity> userIPage = this.page(page, pointUpdateRecordQueryWrapper);
            return pointUpdateRecordPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        }
    }

    public QueryWrapper wrapperHandle(QueryWrapper<?> wrapper, String id, Class<?> aClass, String type, String tableName) {
        try {
            wrapper.apply(" 1=1 ", new Object[0]);

            for(RuleInfo info : this.generaterSwapUtil.getFilterCondition(id)) {
                String field = info.getField();
                if ((!"main".equals(type) || !field.contains("-")) && (!"main".equals(type) || !field.contains("_jnpf_")) && (!"sub".equals(type) || field.contains("-")) && (!"sub-jnpf".equals(type) || field.contains("_jnpf_"))) {
                    String fieldName = field;
                    String table = "";
                    if (field.contains("-")) {
                        fieldName = field.split("-")[1];
                    }

                    if (field.contains("_jnpf_")) {
                        fieldName = field.split("_jnpf_")[1];
                        table = field.split("_jnpf_")[0];
                        table = table.replace("jnpf_", "");
                    }

                    if (!"sub-jnpf".equals(type) || tableName.equals(table)) {
                        Field declaredField = aClass.getDeclaredField(fieldName);
                        declaredField.setAccessible(true);
                        String fieldDb = ((TableField)declaredField.getAnnotation(TableField.class)).value();
                        GenUtil genUtil = (GenUtil)JsonUtil.getJsonToBean(info, GenUtil.class);
                        genUtil.solveValue(wrapper, fieldDb);
                    }
                }
            }

            return wrapper;
        } catch (Exception var15) {
            return wrapper;
        }
    }

    public PointUpdateRecordEntity getInfo(String id) {
        QueryWrapper<PointUpdateRecordEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(PointUpdateRecordEntity::getId, id);
        return (PointUpdateRecordEntity)this.getOne(queryWrapper);
    }

    public void create(PointUpdateRecordEntity entity) {
        this.save(entity);
    }

    public boolean update(String id, PointUpdateRecordEntity entity) {
        return this.updateById(entity);
    }

    public void delete(PointUpdateRecordEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }

    }

    public String checkForm(PointUpdateRecordForm form, int i) {
        int total = 0;
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        String id = "";
        String countRecover = "";
        if (isUp) {
            id = form.getId();
        }

        return countRecover;
    }
}
